#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   BOOT_WriteDataByIdentifier_0x2E_test.py
@Time    :   2025/04/10 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangl@genvict.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

import pytest
import logging
import time
import allure
import sys
from pytest_assume.plugin import assume
import Set_up

sys.dont_write_bytecode = True

# 支持的DID列表 (DID, 数据长度, 描述)
supported_dids = [
    ("F1 84", Set_up.SoftwareFingerprintInformation_hex),                       # 应用软件指纹信息
]

WriteDataByIdentifier_NRC31 = [
    ("F0 90 01", "备份软件版本号"),
    ("F0 32 02", "标定版本号"),
    ("F0 A0 03", "备区Bootloader版本号"),
    ("F0 31 04", "标定零件号"),
    ("F0 F0 05 06", "当前运行分区信息"),
    ("F0 F4 07", "子部件当前运行分区信息"),
    ("F0 F2 02", "获取安装进度和结果"),
    ("FD 32 01", "华为电子标签"),
    ("F1 5C 02", "密钥灌装状态"),
    ("F0 38 03", "无效"),
    ("F0 39 04 05", "无效"),
    ("F0 40 06", "无效"),
    ("F0 36 01", "无效"),
    ("F1 90 00", "车辆识别号"),
    ("F0 FF 00", "读写整车软件版本号"),
    ("F1 A9 00", "电控单元配置文件号"),
]

# 测试用例类
class Test_BOOT_WriteDataByIdentifier_Phy(object):

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-写入DID响应NRC7F')
    @allure.description('测试物理寻址编程会话下写入DID响应NRC7F')
    @pytest.mark.parametrize("did, resp", supported_dids, ids=[f"Request {did[0]}" for did in supported_dids])
    def test_WriteDataByIdentifier_Programming_phy(self, action, Driver, did, resp):
        """测试读取支持的DID"""
        iStep = 1
        # 发送2E服务请求,写入DID
        req = f"2E {did}" + resp
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, f'6E {did}', 2, iStep, True)
        # 发送22服务请求,读取DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, f'22 {did}', f"62 {did}" + resp, 0, iStep, False)
        # 重启后再次读取DID
        iStep = Driver['CAN'].restartETC_Power(Driver, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, f'22 {did}', f"62 {did}" + resp, 0, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-写入不支持的DID响应NR31')
    @allure.description('测试物理寻址编程会话下写入不支持的DID响应NR31')
    @pytest.mark.parametrize("req, resp", WriteDataByIdentifier_NRC31, ids=[f"Request {did[0]}" for did in WriteDataByIdentifier_NRC31])
    def test_WriteDataByIdentifier_NRC31_Programming_phy(self, action, Driver, req, resp):
        """测试读取支持的DID"""
        iStep = 1
        # 发送2E服务请求,写入DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, f'2E {req}', '7F 2E 31', 2, iStep, True)

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-写入长度错误的DID响应NR13')
    @allure.description('测试物理寻址编程会话下写入长度错误的DID响应NR13')
    def test_WriteDataByIdentifier_NRC13_Programming_phy(self, action, Driver):
        """测试读取支持的DID"""
        iStep = 1
        # 发送2E服务请求,写入DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 84 00 00', '7F 2E 13', 2, iStep, True)

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-未安全解锁写入DID响应NR33')
    @allure.description('测试物理寻址编程会话下未安全解锁写入DID响应NR33')
    def test_WriteDataByIdentifier_NRC33_Programming_phy(self, action, Driver):
        """测试读取支持的DID"""
        iStep = 1
        # 发送2E服务请求,写入DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 84 00 00', '7F 2E 33', 2, iStep, False)

    # @allure.severity('normal')
    # @allure.title('物理寻址-编程会话-车速大于3kph写入DID响应NR22')
    # @allure.description('测试物理寻址编程会话下车速大于3kph写入DID响应NR22')
    # def test_WriteDataByIdentifier_NRC22_Programming_phy(self, action, Driver):
    #     """测试读取支持的DID"""
    #     iStep = 1
    #     # 
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 2, iStep, True)
    #     # 设置车速为8kph
    #     iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
    #     # 发送2E服务请求,写入DID
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, f'2E F1 84' + Set_up.SoftwareFingerprintInformation_hex, '7F 2E 22', 0, iStep, False)
    #     # 恢复车速为0kph
    #     iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-写入长度错误的DID响应NR13')
    @allure.description('测试物理寻址编程会话下写入长度错误的DID响应NR13')
    def test_WriteDataByIdentifier_NRC13andNRC31_Priority_Programming_phy(self, action, Driver):
        """测试读取支持的DID"""
        iStep = 1
        # 发送2E服务请求,写入DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 90', '7F 2E 13', 2, iStep, True)

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-未安全解锁写入长度错误的DID响应NR33')
    @allure.description('测试物理寻址编程会话下未安全解锁写入长度错误的DID响应NR33')
    def test_WriteDataByIdentifier_NRC33andNRC13_Priority_Programming_phy(self, action, Driver):
        """测试读取支持的DID 诊断调查表里33优先级高于13"""
        iStep = 1
        # 发送2E服务请求,写入DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 90', '7F 2E 33', 2, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-未安全解锁写入错误的DID响应NR33')
    @allure.description('测试物理寻址编程会话下未安全解锁写入错误的DID响应NR33')
    def test_WriteDataByIdentifier_NRC33andNRC31_Priority_Programming_phy(self, action, Driver):
        """测试读取支持的DID 诊断调查表33优先级高于31"""
        iStep = 1
        # 发送2E服务请求,写入DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 99 00 01', '7F 2E 33', 2, iStep, False)    

    # @allure.severity('normal')
    # @allure.title('物理寻址-编程会话-车速大于3kph写入长度错误的DID响应NR13')
    # @allure.description('测试物理寻址编程会话下车速大于3kph写入长度错误的DID响应NR13')
    # def test_WriteDataByIdentifier_NRC13andNRC22_Priority_Programming_phy(self, action, Driver):
    #     """测试读取支持的DID"""
    #     iStep = 1
    #     # 发送2E服务请求,写入DID
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '2E 00', 2, iStep, True)
    #     # 设置车速为8kph
    #     iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
    #     # 发送2E服务请求,写入DID
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 90', '7F 2E 13', 0, iStep, False)
    #     # 恢复车速为0kph
    #     iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

class Test_APP_WriteDataByIdentifier_Func(object):

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-写入DID响应NRC7F')
    @allure.description('测试物理寻址编程会话下写入DID响应NRC7F')
    @pytest.mark.parametrize("did, resp", supported_dids, ids=[f"Request {did[0]}" for did in supported_dids])
    def test_WriteDataByIdentifier_Programming_func(self, action, Driver, did, resp):
        """测试读取支持的DID"""
        iStep = 1
        # 发送2E服务请求,写入DID
        req = f"2E {did}" + resp
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 2, iStep, True, 'UDSFun')

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-写入不支持的DID响应NR31')
    @allure.description('测试物理寻址编程会话下写入不支持的DID响应NR31')
    @pytest.mark.parametrize("req, resp", WriteDataByIdentifier_NRC31, ids=[f"Request {did[0]}" for did in WriteDataByIdentifier_NRC31])
    def test_WriteDataByIdentifier_NRC31_Programming_func(self, action, Driver, req, resp):
        """测试读取支持的DID"""
        iStep = 1
        # 发送2E服务请求,写入DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, f'2E {req}', '', 2, iStep, True, 'UDSFun')

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-写入长度错误的DID响应NR13')
    @allure.description('测试物理寻址编程会话下写入长度错误的DID响应NR13')
    def test_WriteDataByIdentifier_NRC13_Programming_func(self, action, Driver):
        """测试读取支持的DID"""
        iStep = 1
        # 发送2E服务请求,写入DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 84 00 00', '', 2, iStep, True, 'UDSFun')

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-未安全解锁写入DID响应NR33')
    @allure.description('测试物理寻址编程会话下未安全解锁写入DID响应NR33')
    def test_WriteDataByIdentifier_NRC33_Programming_func(self, action, Driver):
        """测试读取支持的DID"""
        iStep = 1
        # 发送2E服务请求,写入DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 84 00 00', '', 2, iStep, False, 'UDSFun')

    # @allure.severity('normal')
    # @allure.title('物理寻址-编程会话-车速大于3kph写入DID响应NR22')
    # @allure.description('测试物理寻址编程会话下车速大于3kph写入DID响应NR22')
    # def test_WriteDataByIdentifier_NRC22_Programming_phy(self, action, Driver):
    #     """测试读取支持的DID"""
    #     iStep = 1
    #     # 
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 2, iStep, True)
    #     # 设置车速为8kph
    #     iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
    #     # 发送2E服务请求,写入DID
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, f'2E F1 84' + Set_up.SoftwareFingerprintInformation_hex, '7F 2E 22', 0, iStep, False)
    #     # 恢复车速为0kph
    #     iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-写入长度错误的DID响应NR13')
    @allure.description('测试物理寻址编程会话下写入长度错误的DID响应NR13')
    def test_WriteDataByIdentifier_NRC13andNRC31_Priority_Programming_func(self, action, Driver):
        """测试读取支持的DID"""
        iStep = 1
        # 发送2E服务请求,写入DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 90', '', 2, iStep, True, 'UDSFun')

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-未安全解锁写入长度错误的DID响应NR33')
    @allure.description('测试物理寻址编程会话下未安全解锁写入长度错误的DID响应NR33')
    def test_WriteDataByIdentifier_NRC33andNRC13_Priority_Programming_func(self, action, Driver):
        """测试读取支持的DID"""
        iStep = 1
        # 发送2E服务请求,写入DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 90', '', 2, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-未安全解锁写入错误的DID响应NR33')
    @allure.description('测试物理寻址编程会话下未安全解锁写入错误的DID响应NR33')
    def test_WriteDataByIdentifier_NRC33andNRC31_Priority_Programming_func(self, action, Driver):
        """测试读取支持的DID"""
        iStep = 1
        # 发送2E服务请求,写入DID
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 99 00 01', '', 2, iStep, False, 'UDSFun')    

    # @allure.severity('normal')
    # @allure.title('物理寻址-编程会话-车速大于3kph写入长度错误的DID响应NR13')
    # @allure.description('测试物理寻址编程会话下车速大于3kph写入长度错误的DID响应NR13')
    # def test_WriteDataByIdentifier_NRC13andNRC22_Priority_Programming_phy(self, action, Driver):
    #     """测试读取支持的DID"""
    #     iStep = 1
    #     # 发送2E服务请求,写入DID
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '2E 00', 2, iStep, True)
    #     # 设置车速为8kph
    #     iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
    #     # 发送2E服务请求,写入DID
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 90', '7F 2E 13', 0, iStep, False)
    #     # 恢复车速为0kph
    #     iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)